<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Subscribe;
use Illuminate\Http\Request;


class SubscribeController extends BaseController
{

    public function store(Request $request)
    {
        $user_id = $this->getUserId();
        $arr_template_id = $request->input('template_id');

        $comment_status = $arr_template_id[Subscribe::TEMPLATE_COMMENT] ?? 'reject';
        if ($comment_status == 'accept') {
//            Subscribe::where('user_id', $user_id)->where('template_id', Subscribe::TEMPLATE_COMMENT)->delete();
            Subscribe::create([
                'user_id' => $user_id,
                'template_id' => Subscribe::TEMPLATE_COMMENT,
                'type' => Subscribe::TYPE_COMMENT
            ]);
        }

        $article_star_status = $arr_template_id[Subscribe::TEMPLATE_ARTICLE_STAR] ?? 'reject';
        if ($article_star_status == 'accept') {
            Subscribe::where('user_id', $user_id)->where('template_id', Subscribe::TEMPLATE_ARTICLE_STAR)->delete();
            Subscribe::create([
                'user_id' => $user_id,
                'template_id' => Subscribe::TEMPLATE_ARTICLE_STAR,
                'type' => Subscribe::TYPE_ARTICLE_STAR
            ]);
        }

        $article_notice_status = $arr_template_id[Subscribe::TEMPLATE_CIRCLE_ARTICLE_NOTICE] ?? 'reject';
        if ($article_notice_status == 'accept') {
            Subscribe::where('user_id', $user_id)->where('template_id', Subscribe::TEMPLATE_CIRCLE_ARTICLE_NOTICE)->delete();
            Subscribe::create([
                'user_id' => $user_id,
                'template_id' => Subscribe::TEMPLATE_CIRCLE_ARTICLE_NOTICE,
                'type' => Subscribe::TYPE_CIRCLE_ARTICLE_NOTICE
            ]);
        }

        $sign_in_notice_status = $arr_template_id[Subscribe::TEMPLATE_SIGN_IN] ?? 'reject';
        if ($sign_in_notice_status == 'accept') {
            Subscribe::where('user_id', $user_id)->where('template_id', Subscribe::TEMPLATE_SIGN_IN)->delete();
            Subscribe::create([
                'user_id' => $user_id,
                'template_id' => Subscribe::TEMPLATE_SIGN_IN,
                'type' => Subscribe::TYPE_SIGN_IN
            ]);
        }

        $sign_in_notice_status = $arr_template_id[Subscribe::TEMPLATE_BIRTHDAY] ?? 'reject';
        if ($sign_in_notice_status == 'accept') {
            Subscribe::where('user_id', $user_id)->where('template_id', Subscribe::TEMPLATE_BIRTHDAY)->delete();
            Subscribe::create([
                'user_id' => $user_id,
                'template_id' => Subscribe::TEMPLATE_BIRTHDAY,
                'type' => Subscribe::TYPE_BIRTHDAY
            ]);
        }


        $sign_in_notice_status = $arr_template_id[Subscribe::TEMPLATE_ACTIVITY_START] ?? 'reject';
        if ($sign_in_notice_status == 'accept') {
            Subscribe::create([
                'user_id' => $user_id,
                'template_id' => Subscribe::TEMPLATE_ACTIVITY_START,
                'type' => Subscribe::TYPE_ACTIVITY_START
            ]);
        }

        $sign_in_notice_status = $arr_template_id[Subscribe::TEMPLATE_ACTIVITY_END] ?? 'reject';
        if ($sign_in_notice_status == 'accept') {
            Subscribe::create([
                'user_id' => $user_id,
                'template_id' => Subscribe::TEMPLATE_ACTIVITY_END,
                'type' => Subscribe::TYPE_ACTIVITY_END
            ]);
        }

        $sign_in_notice_status = $arr_template_id[Subscribe::TEMPLATE_ACTIVITY_COUNT_DOWN] ?? 'reject';
        if ($sign_in_notice_status == 'accept') {
            Subscribe::create([
                'user_id' => $user_id,
                'template_id' => Subscribe::TEMPLATE_ACTIVITY_COUNT_DOWN,
                'type' => Subscribe::TYPE_ACTIVITY_COUNT_DOWN
            ]);
        }


        $sign_in_notice_status = $arr_template_id[Subscribe::TEMPLATE_ORDER_DELIVER] ?? 'reject';
        if ($sign_in_notice_status == 'accept') {
            Subscribe::create([
                'user_id' => $user_id,
                'template_id' => Subscribe::TEMPLATE_ORDER_DELIVER,
                'type' => Subscribe::TYPE_ORDER_DELIVER
            ]);
        }

        return $this->success([]);
    }
}